/*
 * Utils.cpp
 *
 *  Created on: 28.02.2012
 *      Author: Caffeine
 */

#include "Utils.h"
#include "log/Log.h"
#include <cstdarg>
#include <cstdio>
#include "log/Log.h"
#include "core/Environment.h"
#include "StringUtils.h"
namespace irr {
namespace utils{



irr::video::IImage* TextureImage(irr::video::ITexture* texture,irr::video::IVideoDriver*driver) {
    if (!driver){
    	LOGE("Driver is NULL");
    	exit(0);
    	return 0;
    }
    if (!texture){
    	LOGE("Texture is NULL");
    	exit(0);
    	return 0;
    }
	irr::video::IImage* image = driver->createImageFromData (
			texture->getColorFormat(),
			texture->getSize(),
			texture->lock(),
			false  //copy mem
	);
	texture->unlock();
	return image;
}

irr::video::ITexture* ImageTexture(irr::video::IImage* image,irr::video::IVideoDriver*driver,irr::core::stringc name) {
    if (!driver){
    	LOGE("Driver is NULL");
    	exit(0);
    	return 0;
    }
	if (!image){
		LOGE("Image is NULL");
		return 0;
	}
	irr::video::ITexture* texture = driver->addTexture(name.c_str(),image);
	if (!texture){
		LOGE("Can't create texture with name=\"%s\"",name.c_str());
		return 0;
	}
	texture->grab();
	return texture;
}

bool fileExist(irr::core::stringc path){
    return irr::core::Environment::getDevice()->getFileSystem()->existFile(path);
}

/*
void cp1251_to_utf8(char *out, const char *in) {
    static const int table[128] = {
        0x82D0,0x83D0,0x9A80E2,0x93D1,0x9E80E2,0xA680E2,0xA080E2,0xA180E2,
        0xAC82E2,0xB080E2,0x89D0,0xB980E2,0x8AD0,0x8CD0,0x8BD0,0x8FD0,
        0x92D1,0x9880E2,0x9980E2,0x9C80E2,0x9D80E2,0xA280E2,0x9380E2,0x9480E2,
        0,0xA284E2,0x99D1,0xBA80E2,0x9AD1,0x9CD1,0x9BD1,0x9FD1,
        0xA0C2,0x8ED0,0x9ED1,0x88D0,0xA4C2,0x90D2,0xA6C2,0xA7C2,
        0x81D0,0xA9C2,0x84D0,0xABC2,0xACC2,0xADC2,0xAEC2,0x87D0,
        0xB0C2,0xB1C2,0x86D0,0x96D1,0x91D2,0xB5C2,0xB6C2,0xB7C2,
        0x91D1,0x9684E2,0x94D1,0xBBC2,0x98D1,0x85D0,0x95D1,0x97D1,
        0x90D0,0x91D0,0x92D0,0x93D0,0x94D0,0x95D0,0x96D0,0x97D0,
        0x98D0,0x99D0,0x9AD0,0x9BD0,0x9CD0,0x9DD0,0x9ED0,0x9FD0,
        0xA0D0,0xA1D0,0xA2D0,0xA3D0,0xA4D0,0xA5D0,0xA6D0,0xA7D0,
        0xA8D0,0xA9D0,0xAAD0,0xABD0,0xACD0,0xADD0,0xAED0,0xAFD0,
        0xB0D0,0xB1D0,0xB2D0,0xB3D0,0xB4D0,0xB5D0,0xB6D0,0xB7D0,
        0xB8D0,0xB9D0,0xBAD0,0xBBD0,0xBCD0,0xBDD0,0xBED0,0xBFD0,
        0x80D1,0x81D1,0x82D1,0x83D1,0x84D1,0x85D1,0x86D1,0x87D1,
        0x88D1,0x89D1,0x8AD1,0x8BD1,0x8CD1,0x8DD1,0x8ED1,0x8FD1
    };
    while (*in)
        if (*in & 0x80) {
            int v = table[(int)(0x7f & *in++)];
            if (!v)
                continue;
            *out++ = (char)v;
            *out++ = (char)(v >> 8);
            if (v >>= 16)
                *out++ = (char)v;
        }
        else
            *out++ = *in++;
    *out = 0;
}
*/

void copyImageWithAlpha(irr::video::IImage*from,irr::video::IImage*to,irr::core::vector2di pos){
	from->grab();
	to->grab();
    irr::core::dimension2d<irr::u32> size=from->getDimension();//�������� ������ �������� ������� ����� �����������
    //irr::core::vector2di max(size.Width,size.Height);

    for (irr::u32 x=pos.X;x<pos.X+size.Width;x++)
        for (irr::u32 y=pos.Y;y<pos.Y+size.Height;y++){
            irr::video::SColor f=from->getPixel(x-pos.X,y-pos.Y);//������
            irr::video::SColor t=to->getPixel(x,y);//����



            float a=f.getAlpha()/255.0f;//�������� �������� ������������ ��������, ������� ���������
            float b=1.0f-a;//�������� ������������ (��� ��� ��� �� ��������? ��������������?)
            //t.setAlpha(core::max_(t.getAlpha()+f.getAlpha(),255u));
            t.setAlpha(core::max_(t.getAlpha(),f.getAlpha()));
            t.setRed((f.getRed()*a)+t.getRed()*b);//��������� �������
            t.setGreen((f.getGreen()*a)+t.getGreen()*b);//��������� ������
            t.setBlue((f.getBlue()*a)+t.getBlue()*b);//��������� �����


            to->setPixel(x,y,t);//������������� ������� �� ������� ���� ���������


        }
    to->drop();
    from->drop();
}

template <class T>
irr::core::rect<T> rect_norm(irr::core::rect<T> r){
	return irr::core::rect<T>(irr::core::vector2d<T>(0,0),r.UpperLeftCorner-r.LowerRightCorner);
}

irr::core::rect<irr::s32> normalize(irr::core::rect<irr::s32> r){
	return rect_norm<irr::s32>(r);
}
irr::core::rect<irr::u32> normalize(irr::core::rect<irr::u32> r){
	return rect_norm<irr::u32>(r);
}
irr::core::rect<irr::f32> normalize(irr::core::rect<irr::f32> r){
	return rect_norm<irr::f32>(r);
}

template <class T>
irr::core::rect<T> multiplySize_S(irr::core::rect<T> r,T d){
	return irr::core::rect<T>(r.UpperLeftCorner,r.LowerRightCorner*d);
}

irr::core::rect<irr::s32> multiplySize(irr::core::rect<irr::s32> r,irr::s32 d){
	return multiplySize_S<irr::s32>(r,d);
}
irr::core::rect<irr::u32> multiplySize(irr::core::rect<irr::u32> r,irr::u32 d){
	return multiplySize_S<irr::u32>(r,d);
}
irr::core::rect<irr::f32> multiplySize(irr::core::rect<irr::f32> r,irr::f32 d){
	return multiplySize_S<irr::f32>(r,d);
}

irr::io::path getAbsolutePath(irr::io::path path){
	if (path=="")
		return "";
	return irr::core::Environment::getFileSystem()->getAbsolutePath(path);
}
/*
irr::core::rect<int> multiplySize(irr::core::rect<int> r,int d){
	return multiplySize_S<int>(r,d);
}
*/
}//namespace utils
}//namespace irr
